let common = require('../../../utils/common.js');
// pages/activity/book/book.js
Page({
  data: {
    detail: {}
  },
  onLoad: function (option) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      detail: JSON.parse(option.detail)
    });
  },
  bookSubmit: function (e) {
    let frmData = this.extendForm(e.detail.value), that = this;
    if (frmData) {
      common.asynPost(common.API.activity.book, frmData, function (data) {
        if (data['result']) {
          let isApply = data['result'].isApply;
          if (isApply == 1) {
            wx.showToast({ title: '活动报名未开始' });
          } else if (isApply == 2) {
            wx.showToast({ title: '活动报名已结束' });
          } else {
            var detail = that.data.detail;
            var result = {
              id: data['result'].id,
              cover: detail.cover,
              title: detail.title,
              adddress: detail.address,
              time: detail.startTime,
              name: frmData.name,
              phone: frmData.tel,
              code: data['result'].code,
              applyCode: data['result'].applyCode,
              price: data['result'].price
            }
            wx.redirectTo({
              url: '../../pay/bookedinfo/bookedinfo?detail=' + JSON.stringify(result)
            });
          }
        }
      });
    }else{
      wx.showToast({
       title:'请填写完整的预约信息',
       mask:true,
       duration:2000
     });
    }
  },
  extendForm: function (frm) {
    if (!frm.name) return;
    if (!frm.phone) return;
    if (!frm.address) return;
    return {
      activityId: this.data.detail.id,
      price: this.data.detail.price,
      name: frm.name,
      tel: frm.phone,
      address: frm.address
    };
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})